﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

namespace yq.ControllerBase.CaChe
{
    /// <summary>
    /// 权限本地缓存
    /// </summary>
    public class PurviewCaChe : ICaChe
    {
        static object lockCache = new object();

        public override T ReadCache<T>(string key)
        {
            yq.Model.Logic.PurviewPage page = new Model.Logic.PurviewPage();
            Hashtable hash = CacheHelper.GetCache(this.CacheName) as Hashtable ?? new Hashtable();
            if (hash.ContainsKey(key) == false)
            {
                page = yq.Model.Logic.PurviewPage.Load(key, Basis.CaCheLocdType.CaChe);

                //写入缓存
                WriteCache(key, page);
            }
            else
            {
                page = hash[key] as yq.Model.Logic.PurviewPage;
            }

            return page as T;
        }

        /// <summary>
        /// 写入本地缓存
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        private void WriteCache(string key, object obj)
        {
            lock (lockCache)
            {
                Hashtable hash = CacheHelper.GetCache(this.CacheName) as Hashtable ?? new Hashtable();
                hash[hash] = obj;
               // hash.Add(key, obj);
                CacheHelper.SetCache(this.CacheName, hash, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0,CacheTime,0));
            }
        }

    }
}
